package com.pro.schedule.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pro.schedule.entity.Event;
import com.pro.schedule.entity.EventType;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Mapper
public interface EventMapper extends BaseMapper<Event> {
    @Select("select * from event where user_id=#{userId} and event_date=#{eventDate}")
    @Results(
            {
                    @Result(column = "event_id", property = "eventId"),
                    @Result(column = "event_title", property = "eventTitle"),
                    @Result(column = "event_date", property = "eventDate"),
                    @Result(column = "event_start", property = "eventStart"),
                    @Result(column = "event_end", property = "eventEnd"),
                    @Result(column = "event_type_id", property = "eventTypeId"),
                    @Result(column = "event_notes", property = "eventNotes"),
                    @Result(column = "event_color", property = "eventColor"),
                    @Result(column = "user_id", property = "userId"),
                    @Result(column = "target_id", property = "targetId"),
                    @Result(column = "event_type_id", property = "eventType", javaType = EventType.class,
                            one = @One(select = "com.pro.schedule.mapper.EventTypeMapper.selectById")
                    )
            }
    )
    List<Event> getEventList(Long userId, LocalDate eventDate);

    @Select("select * from event where user_id=#{userId} and event_start<=#{eventDateTime} and event_end>=#{eventDateTime}")
    Event getEventInfo(Long userId, LocalDateTime eventDateTime);

    @Delete("delete from event where user_id=#{userId} and event_date=#{eventDate}")
    int clearEvent(Long userId, LocalDate eventDate);

    @Update("update event set target_id = NULL where target_id=#{targetId}")
    void clearEventByTargetId(Integer targetId);
    @Update("update event set event_type_id = NULL where event_type_id=#{eventTypeId}")
    void clearEventByEventTypeId(Integer eventTypeId);
    @Update("update event set target_id = #{targetId} where user_id=#{userId} and event_title=#{targetDetail} and target_id is null")
    void updateEventByTargetId(Integer targetId,Long userId, String targetDetail);

    @Select("select count(*) from event where event_start>=#{startDateTime} and event_end<=#{endDateTime} and target_id=#{targetId}")
    Integer getEventCount(LocalDateTime startDateTime,LocalDateTime endDateTime,Integer targetId);
}
